﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frogism.Core;
using Frogism.Domain;
using NUnit.Framework;
using Frogism.Data._2;

[TestFixture]
public class BookRepositoryTests {
  [Test]
  public void GetReadById() {
    Frogism.Core.IBookManager repos = new BookRepository();
    IBook book = repos.Get(2);
    IRead read = repos.GetReadById(book, book.Reads[0].Id);
    Assert.AreEqual(book.Reads[0].Id, read.Id);
    Assert.AreEqual(book.Reads[0].Page, read.Page);
    Assert.AreEqual(book.Reads[0].ReadDate, read.ReadDate);
  }
  [Test]
  public void GetList() {
    Frogism.Core.IBookManager repos = new BookRepository();
    Assert.IsNotNull(repos.GetAll());
    Assert.Less(0, repos.GetAll().Count());
  }
  [Test]
  public void CreateRepository() {
    Assert.IsNotNull(new BookRepository());
  }
  [Test]
  public void GetId2() {
    Frogism.Core.IBookManager repos = new BookRepository();
    Assert.IsNotNull(repos.Get(2));
    Assert.AreEqual(2, repos.Get(2).Id);
  }
  [Test]
  public void GetId2HasAuthor() {
    Frogism.Core.IBookManager repos = new BookRepository();
    IBook bo = repos.Get(2);

    Assert.IsNotNull(bo.Author);
    Assert.IsNotNull(bo.Author.Name);
    Assert.AreEqual("Meszaros, Gerard", bo.Author.Name);


  }
  [Test]
  public void GetCurrent() {
    IBookManager repos = new BookRepository();
    IEnumerable<IBook> books = repos.GetReading();

    Assert.IsNotNull(books);
    Assert.Less(0, books.Count());
    foreach (IBook b in books) {
      Assert.IsFalse(b.IsFinished);
    }
  }
  [Test]
  public void GetFinished() {
    IBookManager repos = new BookRepository();
    IEnumerable<IBook> books = repos.GetFinished();

    Assert.IsNotNull(books);
    Assert.Less(0, books.Count());
    foreach (Book b in books) {
      Assert.IsTrue(b.IsFinished);
    }
  }
  [Test]
  public void Id2HasReads() {
    IBookManager repos = new BookRepository();
    IBook bo = repos.Get(2);
    Assert.IsNotNull(bo.Reads);
    Assert.Less(0, bo.Reads.Count());
  }
  //[Test]
  //public void AllAuthors() {
  //  IBookManager repos = new BookRepository();
  //  Assert.IsNotNull(repos.GetAllAuthors());
  //  Assert.Less(0, repos.GetAllAuthors().Count());
  //}
    [Test]
    public void NewBookManager() {
      IBookManager bm = ManagerService.GetBookManager();
    }
    [Test]
    public void CheckYearList() {
      IBookManager bm = ManagerService.GetBookManager();
      Collection<int> years = bm.YearList;
      Assert.IsNotNull(years);
      Assert.Less(0, years.Count());
      Assert.AreEqual(DateTime.Now.Year, years[0]);
    }
    [Test]
    public void GetPublicBooks() {
      IBookManager bm = ManagerService.GetBookManager();
      IEnumerable<IBook> books = bm.GetPublicReading();
      foreach (IBook b in books) {
        Assert.IsFalse(b.IsFinished, "book{" + b.Title + "} is Finished.");
        Assert.IsFalse(b.IsPrivate, "book{" + b.Title + "} is Private.");
      }
    }

    [Test]
    public void UpdateTitle() {
      IBookManager bm = ManagerService.GetBookManager();

      IBook book = bm.Get(2);
      string tempTitle = book.Title;
      book.Title = "XUnit Test Title";

      bm.Update(book);

      IBook book2 = bm.Get(2);
      Assert.AreEqual("XUnit Test Title", book2.Title);

      book2.Title = tempTitle;
      bm.Update(book2);


    }
    [Test]
    public void UpdateRead() {
      IBookManager bm = ManagerService.GetBookManager();

      IBook book = bm.Get(2);
      IRead read = book.Reads[book.Reads.Count - 1];
      int tempPage = read.Page;
      read.Page = 9999;
      bm.UpdateRead(book, read);

      IBook book2 = bm.Get(2);
      IRead read2 = book2.Reads[book2.Reads.Count - 1];
      Assert.AreEqual(9999, read2.Page);

      read.Page = tempPage;
      bm.UpdateRead(book, read);
    }
    [Test]
    public void AddRead() {
      IBookManager bm = ManagerService.GetBookManager();

      IBook book = bm.Get(2);
      IRead read = bm.GetOrCreateTodaysRead(book);
      read.Page = 9999;
      bm.UpdateRead(book, read);

      IBook book2 = bm.Get(2);
      IRead read2 = book2.Reads[book2.Reads.Count - 1];
      Assert.AreEqual(9999, read2.Page);


      bm.DeleteRead(read2);
    }
    [Test]
    public void DeleteRead() {
      IBookManager bm = ManagerService.GetBookManager();

      IBook book = bm.Get(2);
      IRead read = book.Reads[book.Reads.Count - 1];
      int tempPage = read.Page;
      read.Page = 9999;
      bm.UpdateRead(book, read);

      IBook book2 = bm.Get(2);
      IRead read2 = book2.Reads[book2.Reads.Count - 1];
      Assert.AreEqual(9999, read2.Page);

      read.Page = tempPage;
      bm.UpdateRead(book, read);
    }

    [Test]
    public void GetAllBooks() {
      IBookManager bm = ManagerService.GetBookManager();
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books);
    }
    [Test]
    public void GetOneBook() {
      IBookManager bm = ManagerService.GetBookManager();
      IBook b = bm.Get(2);
      Assert.IsNotNull(b);
    }
    [Test]
    public void BookTitle() {
      IBookManager bm = ManagerService.GetBookManager();
      IBook b = bm.Get(2);
      Assert.IsNotNull(b.Title);
    }
    [Test]
    public void BookAuthor() {
      IBookManager bm = ManagerService.GetBookManager();
      IBook b = bm.Get(2);
      Assert.IsNotNull(b.Author);
    }
    [Test]
    public void BookTotalPages() {
      IBookManager bm = ManagerService.GetBookManager();
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books.First().TotalPages);
      Assert.Greater(books.First().TotalPages, 0);
    }
    [Test]
    public void Read() {
      IBookManager bm = ManagerService.GetBookManager();
      IEnumerable<IBook> books = bm.GetAll();
      Assert.IsNotNull(books.First().Reads);
    }
    [Test]
    public void TestReadNotEmpty() {
      IBookManager bm = ManagerService.GetBookManager();
      IBook book = bm.Get(2);
      Assert.Greater(book.Reads.Count, 0);
    }
    [Test]
    public void CurrentPage() {
      IBook book = new Book();
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      read = new Read();
      read.Page = 99;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(99, book.TotalRead);
    }
    [Test]
    public void PagesToGo() {
      Book book = new Book();
      book.LastPageNumber = 100;
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Read read2 = new Read();
      read2.Page = 88;
      read2.ReadDate = DateTime.Now;
      book.Reads.Add(read2);

      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(book.TotalPages - read2.Page, book.ToGo);
    }
    [Test]
    public void TotalReadWithZeroReads() {
      IBook book = new Book();
      book.LastPageNumber = 101;

      Assert.AreEqual(0, book.TotalRead);
    }

    [Test]
    public void PercentRead() {
      Book book = new Book();
      book.LastPageNumber = 101;
      Read read = new Read();
      read.Page = 1;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Read read2 = new Read();
      read2.Page = 88;
      read2.ReadDate = DateTime.Now;
      book.Reads.Add(read2);

      read = new Read();
      read.Page = 55;
      read.ReadDate = DateTime.Now;
      book.Reads.Add(read);

      Assert.AreEqual(88.0 / 101.0 * 100.0, book.PercentRead);
    }

  }
